#include <iostream>
#include <signal.h>

#include "cat.h"
#include "sunnet.h"

using namespace std;

int test()
{
    auto pingtype = make_shared<string>("ping");
    uint32_t ping1 = sunnet::inst->newservice(pingtype);
    uint32_t ping2 = sunnet::inst->newservice(pingtype);
    uint32_t pong = sunnet::inst->newservice(pingtype);

    auto msg1 = sunnet::inst->make_msg(ping1, new char[3]{'h', 'i', '\0'}, 3);
    auto msg2 = sunnet::inst->make_msg(ping2, new char[3]{'h', 'o', '\0'}, 3);

    sunnet::inst->send(pong, msg1);
    sunnet::inst->send(pong, msg2);

    return 0;
}

void test_socket()
{
    int fd = sunnet::inst->Listen(8000, 1);
    usleep(15 * 1000000);

    sunnet::inst->close_conn(fd);
}

void test_echo()
{
    auto t = make_shared<string>("gateway");
    uint32_t gateway = sunnet::inst->newservice(t); // 服务里面有测试代码, 开始监听 8000 端口
}

int main()
{
    // 忽略 SIGPIPE 信号
    signal(SIGPIPE, SIG_IGN);

    // cout << "Hello, world" << endl;

    // Cat cat;
    // cat.age = 1;
    // cat.Say();

    new sunnet();
    sunnet::inst->start();

    // test();
    // test_socket();
    // test_echo();
    auto t = make_shared<string>("main");
    sunnet::inst->newservice(t);

    // 主线程进入休眠
    sunnet::inst->wait();

    return 0;
}